package demo.inoutput;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class InOutStream {
	public static void main(String[] args) throws IOException{
		String filePath = "C:/Users/TayDo/Demo";
		File rootDir = new File(filePath);
		System.out.println("Root of navigation:" + rootDir.getAbsolutePath());
		// check if the root exists as a directory.
		if (!(rootDir.isDirectory())) {
			System.out
					.println("The root of the naviagtion subtree does not exist as a directory!");
			System.exit(0);
		}
		FileNavigator fn = new FileNavigator();
		fn.navigate(rootDir);
		
		System.out.println("");
		File fs = new File(rootDir, "InitialTest.java");
		if(fs.isFile())
		{
			String asd = fs.getName();
			asd.lastIndexOf(".");
			
		}
		FileInputStream fis = new FileInputStream(fs);
		File fd = new File(rootDir, "asd.txt");
		FileOutputStream fos = new FileOutputStream(fd);
		
		byte[] b = new byte[1];
		 int length;
         // copy the file content in bytes
         while ((length = fis.read(b)) > 0) {
//        	 System.out.print(new String(b));
             fos.write(b, 0, length);
         }
         
         String contentAdd = "Add by Dv";
         fos.write(contentAdd.getBytes());
         
         fos.write(13);
    	 Integer a = new Integer(1000);
    	 fos.write(a.toString().getBytes());
    	 
         fis.close();
         fos.close();

	}
}

class FileNavigator {
	public void navigate(File dir) {
		System.out.println(" ");
		System.out.println("Directory " + dir + ":");
		String[] dirContent = dir.list();
		if (dirContent != null)
			for (int i = 0; i < dirContent.length; i++) {
				System.out.println(" " + dirContent[i]);
				File child = new File(dir, dirContent[i]);
				if (child.isDirectory())
					navigate(child);
			}
	}
}